import { HeadersTokenKey, UserInfoKey, RoleAuthKey} from './constants';
import {RoleMenuItem, UserInfo} from "../types/common";
const common = {
    getLocalToken: (): string => {
        let token = ''
        if (window.localStorage) {
            try{
                token = window.localStorage.getItem(HeadersTokenKey) || ''
            }catch(e){

            }
        }
        return token
    },
    setLocalToken(token: string) {
        if (window.localStorage) {
            window.localStorage.setItem(HeadersTokenKey, token)
        }
    },
    removeLocalToken() {
        if (window.localStorage) {
            window.localStorage.removeItem(HeadersTokenKey)
        }
    },
    getLocalUser: (): UserInfo => {
        let userInfo = null;
        if (window.localStorage) {
            try{
                const userStr = window.localStorage.getItem(UserInfoKey);
                if (userStr) {
                    userInfo = JSON.parse(userStr)
                }
            }catch(e){

            }
        }
        return userInfo
    },
    setLocalUser(userInfo: UserInfo) {
        if (window.localStorage) {
            try{
                window.localStorage.setItem(UserInfoKey, JSON.stringify(userInfo || ''))
            }catch(e){

            }
        }
    },
    removeLocalUser() {
        if (window.localStorage) {
            window.localStorage.removeItem(UserInfoKey)
        }
    },
    getLocalRoleAuth: (): RoleMenuItem[] => {
        let roleMenuList;
        if (window.localStorage) {
            try{
                const localStr = window.localStorage.getItem(RoleAuthKey);
                if (localStr) {
                    roleMenuList = JSON.parse(localStr)
                }
            }catch(e){

            }
        }
        return roleMenuList
    },
    setLocalRoleAuth(roleMenuAuth: RoleMenuItem) {
        if (window.localStorage) {
            try{
                window.localStorage.setItem(RoleAuthKey, JSON.stringify(roleMenuAuth || ''))
            }catch(e){

            }
        }
    },
    removeLocalRoleAuth() {
        if (window.localStorage) {
            window.localStorage.removeItem(RoleAuthKey)
        }
    },
}
export default common
